<?php
/**
* Implements requests with JSON output.
*<p>This is a derivation of the <i>CoreRequest</i> class that implements a request with an JSON-formatted
*array as output. The JSON is then sent to the client side that usually will process it with a Javascript
*callback function. Please see the client-side documentation for CoreRequestJson for further details.</p>
*<p>The resulting JSON code has the following format:
*<samp> json = {error:'error string, if any', data: { array_of_data } } </samp>.</p>
*<p>Each element of the array_of_data is set by calls to <i>CoreRequestJson::SetOutput()</i>.</p>
*@package Core
*@subpackage Request
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
abstract class CoreRequestJson extends CoreRequest
{
	private $output_data = array();
	/**
	* Set an output variable for the JSON string.
	*@param string $name Name of the JSON parameter
	*@param mixed $value Data (in PHP format, will be converted internally)
	*/
	final public function SetOutput( $name, $value )
	{
		$this->output_data[ $name ] = $value;
	}
	/**
	* Simply call the abstract <i>Process()</i> method.
	*/
	final public function DoRequest()
	{
		header("Content-type: application/json");
		
		ob_start();
		$this->Process();
		$html = ob_get_contents();
		ob_end_clean();
		echo CoreUtils::Php2Json( array( "error" => $this->GetError(),
					"html" => $html,
					"data" => $this->output_data ) );
	}
	/**
	* Do the processing.
	* No return value. Inside, all output variables shall be set via calls to <i>SetOutput()</i>
	*/
	abstract public function Process();
}
?>